package controller;

import models.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class UserController {

    UserService userService =new UserServiceImpl();

    @RequestMapping(value = "/getUserData",method = RequestMethod.GET)
    public void getUserPage(HttpServletRequest request, HttpSession session, HttpServletResponse response) throws IOException {

        String name = (String) session.getAttribute("username");
        String result = userService.getUserData(name);
        response.getWriter().write(result);
    }

    @RequestMapping(value = "/updateUserData",method = RequestMethod.POST)
    public void updateUserPage(HttpServletRequest request, HttpSession session, HttpServletResponse response) throws IOException {
        User user = new User();
        user.setUname((String)session.getAttribute("username"));
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String status = request.getParameter("banStatus");
        Boolean banStatus = status.equals("true");
        user.setEmail(email);
        user.setPwd(password);
        user.setStatus(banStatus);
        userService.upDataUser(user);

    }


}
